#include <future>
#include <iostream>
#include <thread>
#include <vector>

int compute_value()
{
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return 42;
}

int main()
{
    // 创建 std::future
    std::future<int> fut = std::async(std::launch::async, compute_value);

    // 将 future 转换为 shared_future
    std::shared_future<int> shared_fut = fut.share();

    // 创建多个线程共享同一个结果
    std::vector<std::thread> threads;
    for (int i = 0; i < 3; ++i) {
        threads.emplace_back([shared_fut, i]() mutable {
            int value = shared_fut.get(); // 阻塞直到值可用
            std::cout << "Thread " << i << " got value: " << value << "\n";
        });
    }

    for (auto& t : threads) {
        t.join();
    }

    return 0;
}
